/* test_xerobits.c
**
** Copyright (c) 2006, Damon Orecks <nomadxero@gmail.com>
** All rights reserved.
** 
** Redistribution  and  use  in  source  and  binary  forms,  with  or  without
** modification, are permitted provided that the following conditions are met:
** 
**  * Redistributions of  source code must retain the  above copyright notice,
**    this list of conditions and the following disclaimer.
** 
**  * Redistributions  in  binary  form  must reproduce  the  above  copyright
**    notice,  this list  of conditions  and the  following disclaimer  in the
**    documentation and/or other materials provided with the distribution.
** 
**  * Neither the name  of the Schmoo Group nor the  names of its contributors
**    may be  used to endorse or  promote products derived  from this software
**    without specific prior written permission.
** 
** THIS SOFTWARE IS PROVIDED BY  THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY  EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT  LIMITED TO, THE
** IMPLIED WARRANTIES  OF MERCHANTABILITY AND FITNESS FOR  A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO  EVENT SHALL THE  COPYRIGHT OWNER OR  CONTRIBUTORS BE
** LIABLE  FOR  ANY  DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
** CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT  LIMITED  TO,  PROCUREMENT  OF
** SUBSTITUTE GOODS  OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN
** CONTRACT,  STRICT LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE OR  OTHERWISE)
** ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
**
*/

/* Macro Definitions */

/* File Includes */
#include <stdio.h>
#include <string.h>
#include "xerobits.h"
#include "CUnit/Basic.h"
#include "../src/certref.h"

/* Typedefs, Structs, Enums, Unions, etc. */

/* Static Function Prototypes */

static int init_context_suite( void );
static int clean_context_suite( void );
static void test_context_open( void );
static void test_context_open_null( void );

static int init_cert_suite( void );
static int clean_cert_suite( void );
static void test_cert_init( void );
static void test_cert_init_null( void );
static void test_cert_from_encoding( void );
static void test_cert_from_encoding_null( void );
static void test_cert_find_certificate( void );
static void test_cert_find_certificate_null( void );
static void test_array_from_path( void );
static void test_array_from_path_null( void );
static void test_array_from_buffer( void );
static void test_array_from_buffer_null( void );

/* Global Variable Declarations */

gchar *nomad_cert = "-----BEGIN CERTIFICATE-----\n\
MIIEPTCCAyWgAwIBAgIJALf1cuqDqXt/MA0GCSqGSIb3DQEBBQUAMIGtMQswCQYD\n\
VQQGEwJVSzEXMBUGA1UECBMOQ2FtYnJpZGdlc2hpcmUxEjAQBgNVBAcTCUNhbWJy\n\
aWRnZTEVMBMGA1UEChMMU2NobW9vIEdyb3VwMR8wHQYDVQQLExZDZXJ0aWZpY2F0\n\
aW9uIFNlcnZpY2VzMRUwEwYDVQQDEwxEYW1vbiBPcmVja3MxIjAgBgkqhkiG9w0B\n\
CQEWE25vbWFkeGVyb0BnbWFpbC5jb20wHhcNMDYwOTI2MDQwODE0WhcNMDcwOTI2\n\
MDQwODE0WjCBiTELMAkGA1UEBhMCVUsxFzAVBgNVBAgTDkNhbWJyaWRnZXNoaXJl\n\
MRIwEAYDVQQHEwlDYW1icmlkZ2UxFTATBgNVBAoTDFNjaG1vbyBHcm91cDEfMB0G\n\
A1UECxMWQ2VydGlmaWNhdGlvbiBTZXJ2aWNlczEVMBMGA1UEAxMMRGFtb24gT3Jl\n\
Y2tzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvkved+oe3jVBqX0Y\n\
w0sAAhlWreV5TNDJTgbJyGvjMMxk0piJT5wJkiqCbF14nYYaUyCELiYYUN++2ai3\n\
EORmMF9r2qYs0Df/wVSJyVy0kSuSN+6uPwS8Co+xQUnA6ySOdjoc6obakTR9iTSa\n\
pTSwOHqYuLzDedDdgggykSdbGCNvQmYVTTrKO0Lz/EWSEC8umUacQS+hxbD1GC27\n\
orycJH58YGqX5SEA1VvPqwcYVal/8z5kQSgs1hQDnAO+cAcZtKzPo6ztRNzIKBtL\n\
uPmYzgzOcIiGW5HzJK96itbK5Meed9VT4zbvdvKwLEhktiQMjOd7TlpEkjb2HOF9\n\
6xSg/QIDAQABo4GBMH8wEQYJYIZIAYb4QgEBBAQDAgZAMDwGCWCGSAGG+EIBDQQv\n\
Fi1TVEdFTkNFUlQgR2VuZXJhdGVkIENlcnRpZmljYXRlIGZvciBOb21hZFhlcm8w\n\
DAYDVR0TBAUwAwIBADAeBgNVHREEFzAVgRNub21hZHhlcm9AZ21haWwuY29tMA0G\n\
CSqGSIb3DQEBBQUAA4IBAQBZ6S+cnDbFKuyd7lhrJhX3yfv7C3EQmDOGAc/TiDMO\n\
yOUMu/salUw1YSkEtwbRnFQkbs5D2xo/16ULkF4ruaogfpaTW4MLKM581i3PENuu\n\
xRczR0YhqtR0ESdKet+qPJlOxhlwHK7dcE0GzF8GtB3/JA+jONEjjxphAUwWdy76\n\
YeOLUgYMJPMoIYLW9IysK7qwCy44WQDnS2V9x/BAF6TB5SomqXvaxLTQBD78HuEf\n\
W3fDtRuzawWs299zx3rqQHSkFza9hsh0BMwSnW2JPWq+41IBMWBuvCmPN5RBrAzR\n\
HH6RHMqE1CKz0qC86IDJcDcyJHcvRLlqalTDPCU5tAxe\n\
-----END CERTIFICATE-----\n";
gchar *nomad_issuer = "C=UK, ST=Cambridgeshire, L=Cambridge, O=Schmoo Group, OU=Certification Services, CN=Damon Orecks/emailAddress=nomadxero@gmail.com";
gchar *nomad_serial = "t/Vy6oOpe38=";

/* Function Definitions */

int main( int argc, char *argv[] ) {
  CU_pSuite pSuite = NULL;
  CU_pTest  pTest  = NULL;

  do {
    if( CUE_SUCCESS != CU_initialize_registry() ) {
      break;
    }

    pSuite = CU_add_suite( "cert suite", init_cert_suite, clean_cert_suite );
    if( NULL == pSuite ) {
      break;
    }

    pTest = CU_add_test( pSuite, "P Test XB_Certificate_init()", test_cert_init);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "N Test XB_Certificate_init() NULL", test_cert_init_null);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "P Test XB_Certificate_From_Encoding()", test_cert_from_encoding);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "N Test XB_Certificate_From_Encoding() NULL", test_cert_from_encoding_null);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "P Test XB_GPtrArray_Find_Certificate()", test_cert_find_certificate);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "N Test XB_GPtrArray_Find_Certificate() NULL", test_cert_find_certificate_null);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "P Test XB_GPtrArray_From_Buffer()", test_array_from_buffer);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "N Test XB_GPtrArray_From_Buffer() NULL", test_array_from_buffer_null);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "P Test XB_GPtrArray_From_Path()", test_array_from_path);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "N Test XB_GPtrArray_From_Path() NULL", test_array_from_path_null);
    if( NULL == pTest ) {
      break;
    }

    pSuite = CU_add_suite( "context suite", init_context_suite, clean_context_suite );
    if( NULL == pSuite ) {
      break;
    }

    pTest = CU_add_test( pSuite, "P Test XB_Context_open()", test_context_open);
    if( NULL == pTest ) {
      break;
    }

    pTest = CU_add_test( pSuite, "N Test XB_Context_open() NULL", test_context_open_null);
    if( NULL == pTest ) {
      break;
    }
    
  } while( 0 );

  if( pTest != NULL ) {
    CU_basic_set_mode( CU_BRM_VERBOSE );
    CU_basic_run_tests();
  }

  CU_cleanup_registry();
  return( CU_get_error() );
}

static int init_cert_suite( void ) {
  return( 0 );
}

static int clean_cert_suite( void ) {
  return( 0 );
}

static void test_cert_init( void ) {
  XB_CertificateReference cert;
  XB_Err err;

  cert.issuer = "foo";
  cert.serial = "bar";
  cert.pem    = "baz";
  cert.usage  = "foo again";

  CU_ASSERT_EQUAL( XB_Certificate_Init( &cert ), XB_ERR_NOERR );

  CU_ASSERT_EQUAL( cert.issuer, NULL );
  CU_ASSERT_EQUAL( cert.serial, NULL );
  CU_ASSERT_EQUAL( cert.pem   , NULL );
  CU_ASSERT_EQUAL( cert.usage , NULL );  
}

static void test_cert_init_null( void ) {
  CU_ASSERT_EQUAL( XB_Certificate_Init( NULL ), XB_ERR_INVALID_PARAM );
}

static void test_cert_from_encoding( void ) {
  XB_CertificateReference cert;

  XB_Certificate_Init( &cert );
  XB_Certificate_From_Encoding( &cert, nomad_cert, NULL );
  CU_ASSERT_EQUAL( cert.usage , NULL );
  CU_ASSERT_NOT_EQUAL( cert.issuer, NULL );
  if( cert.issuer != NULL ) {
    CU_ASSERT_EQUAL( strcmp( cert.issuer, nomad_issuer ), 0 );
  }

  CU_ASSERT_NOT_EQUAL( cert.serial, NULL );
  if( cert.serial != NULL ) {
    CU_ASSERT_EQUAL( strcmp( cert.serial, nomad_serial ), 0 );
  }
}

static void test_cert_from_encoding_null( void ) {
  XB_CertificateReference cert;

  CU_ASSERT_EQUAL( XB_Certificate_From_Encoding( NULL, nomad_cert, NULL ), XB_ERR_INVALID_PARAM );
  XB_Certificate_Init( &cert );
  CU_ASSERT_EQUAL( XB_Certificate_From_Encoding( &cert, NULL, NULL ), XB_ERR_INVALID_PARAM );
}

static int init_context_suite( void ) {
  return( 0 );
}

static int clean_context_suite( void ) {
  return( 0 );
}

static void test_context_open( void ) {
  XB_Err err = XB_ERR_NOERR;
  XB_Context context;

  memset( &context, 0, sizeof( XB_Context ) );
  err = XB_Context_Open( &context, NULL );
  
  CU_ASSERT_NOT_EQUAL( context.certificates, NULL );
  CU_ASSERT_EQUAL( g_intern_string( "xerobits" ), g_quark_to_string( quark_xerobits ) );
  CU_ASSERT_EQUAL( g_intern_string( "openssl"  ), g_quark_to_string( quark_openssl  ) );
}

static void test_context_open_null( void ) {
  CU_ASSERT_EQUAL( XB_Context_Open( NULL, NULL ), XB_ERR_INVALID_PARAM );
}

static void test_cert_find_certificate( void ) {
  CU_ASSERT_TRUE( 0 ) /* TEST NOT IMPLEMENTED */
}

static void test_cert_find_certificate_null( void ) {
  CU_ASSERT_TRUE( 0 ) /* TEST NOT IMPLEMENTED */
}

static void test_array_from_path( void ) {
  CU_ASSERT_TRUE( 0 ) /* TEST NOT IMPLEMENTED */
}

static void test_array_from_path_null( void ) {
  CU_ASSERT_TRUE( 0 ) /* TEST NOT IMPLEMENTED */
}

static void test_array_from_buffer( void ) {
  CU_ASSERT_TRUE( 0 ) /* TEST NOT IMPLEMENTED */
}

static void test_array_from_buffer_null( void ) {
  CU_ASSERT_TRUE( 0 ) /* TEST NOT IMPLEMENTED */
}
